package com.centaline.androidsalesblog.fragments.list;

import android.view.View;

import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.adapter.StoreAdapter;
import com.centaline.androidsalesblog.base.BaseFragment;
import com.centaline.androidsalesblog.iml.RefreshListener;
import com.centaline.androidsalesblog.model.businissobject.StoreBo;
import com.centaline.androidsalesblog.widgets.MRecyclerView;
import com.centanet.framework.interfaces.IRecycleViewListener;

import java.util.ArrayList;

/**
 * 门店列表
 * Created by Lihq on 16/2/19.
 */
public class StoreListFragment extends BaseFragment {

    private MRecyclerView mRecyclerView;

    private StoreAdapter storeAdapter;
    private ArrayList<StoreBo> storeBos = new ArrayList<>();
    private RefreshListener refreshListener;
    private int iRefreshType;

    public void setRefreshListener(RefreshListener refreshListener) {
        this.refreshListener = refreshListener;
    }

    @Override
    protected int setLayoutId() {
        return R.layout.activity_store_list;
    }

    @Override
    protected void findViews(View view) {
        mRecyclerView = (MRecyclerView) view.findViewById(R.id.mRecyclerView);
    }

    @Override
    protected void initView() {
        storeAdapter = new StoreAdapter(getActivity(), storeBos);
        mRecyclerView.setAdapter(storeAdapter);

        mRecyclerView.setIRecycleViewListener(new IRecycleViewListener() {
            @Override
            public void downRefresh() {
                iRefreshType = 1;
                if (refreshListener != null)
                    refreshListener.down();
            }

            @Override
            public void upRefresh() {
                iRefreshType = 2;
                if (refreshListener != null)
                    refreshListener.up(storeBos.size());
            }

            @Override
            public void onItemClick(int position) {

            }
        });
        startRefresh();
    }

    /**
     * 开启下拉刷新
     */
    public void startRefresh() {
        mRecyclerView.startRefresh();
    }

    /**
     * 停止刷新
     */
    public void stopRefresh(ArrayList<StoreBo> temp) {
        if (iRefreshType == 1)
            storeBos.clear();
        if (temp == null) {
            mRecyclerView.stopRefresh(false);
        } else {
            storeBos.addAll(temp);
            mRecyclerView.stopRefresh(temp.size() == 10);
        }
    }

    public void stopRefresh() {
        storeBos.clear();
        mRecyclerView.stopRefresh(false);
    }

    /**
     * 刷新失败
     */
    public void refreshError() {
        mRecyclerView.refreshError();
    }
}
